package com.ibeeking.found.msg.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.msg.MsgReceiverTypeEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.ISysMsgConfigService;
import com.ibeeking.found.msg.service.common.bo.SysMsgConfigBO;
import com.ibeeking.found.msg.service.common.dos.SysMsgConfigDO;
import com.ibeeking.found.msg.service.common.dos.SysMsgTemplateDO;
import com.ibeeking.found.msg.service.common.param.SysMsgConfigParam;
import com.ibeeking.found.msg.service.common.query.SysMsgConfigQuery;
import com.ibeeking.found.msg.service.common.vo.SysMsgConfigVO;
import com.ibeeking.found.msg.service.mapper.SysMsgConfigMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SysMsgConfigServiceImpl
 * description 邮件服务器配置服务
 * @Author ibeeking
 * @Date 2020-12-09 10:54
 */
@Service
public class SysMsgConfigServiceImpl extends ServiceImpl<SysMsgConfigMapper, SysMsgConfigDO> implements ISysMsgConfigService {


    @Resource
    private SysMsgConfigMapper sysMsgConfigMapper;
    /**
     * 系统消息配置列表
     *
     * @param
     * @return
     */
    @Override
    public List<SysMsgConfigVO> list(SysMsgConfigQuery query) {
        List<SysMsgConfigDO> sysMsgConfigDOs = sysMsgConfigMapper.selectList(new LambdaQueryWrapper<SysMsgConfigDO>()
                .eq(query.getId() != null, SysMsgConfigDO::getId, query.getId())
                .eq(StringUtils.isNotBlank(query.getName()), SysMsgConfigDO::getName, query.getName())
                .eq(query.getReceiverType() != null , SysMsgConfigDO::getReceiverType, query.getReceiverType())
                .eq(query.getPublishStatus() != null, SysMsgConfigDO::getPublishStatus, query.getPublishStatus())
        );
        List<SysMsgConfigVO> sysMsgConfigVOs = JsonUtils.jsonToList(sysMsgConfigDOs, SysMsgConfigVO.class);
        List<SysMsgConfigVO> SysMsgConfigResultVOS = new ArrayList<>();
        if(Collections3.isNotEmpty(sysMsgConfigVOs)){
            SysMsgConfigResultVOS = sysMsgConfigVOs.stream().map(d-> {
                SysMsgConfigVO sysMsgConfigVO = BeanUtil.convertBean(d, SysMsgConfigVO.class);
                sysMsgConfigVO.setPublishStatusName(PublishStatusEnum.getName(d.getPublishStatus()));
                return sysMsgConfigVO;
            }).collect(Collectors.toList());
        }
        return SysMsgConfigResultVOS;
    }
    /**
     * 系统消息配置列表
     *
     * @param
     * @return
     */
    @Override
    public List<SysMsgConfigVO> listAll() {
        List<SysMsgConfigDO> sysMsgConfigDOs = sysMsgConfigMapper.selectList(new LambdaQueryWrapper<SysMsgConfigDO>());
        List<SysMsgConfigVO> sysMsgConfigVOs = JsonUtils.jsonToList(sysMsgConfigDOs, SysMsgConfigVO.class);
        List<SysMsgConfigVO> SysMsgConfigResultVOS = new ArrayList<>();
        if(Collections3.isNotEmpty(sysMsgConfigVOs)){
            SysMsgConfigResultVOS = sysMsgConfigVOs.stream().map(d-> {
                SysMsgConfigVO sysMsgConfigVO = BeanUtil.convertBean(d, SysMsgConfigVO.class);
                sysMsgConfigVO.setPublishStatusName(PublishStatusEnum.getName(d.getPublishStatus()));
                return sysMsgConfigVO;
            }).collect(Collectors.toList());
        }
        return SysMsgConfigResultVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(SysMsgConfigParam param) {
        Long userId = UserUtils.getUserId();

        SysMsgConfigDO sysMsgConfigDO = BeanUtil.convertBean(param, SysMsgConfigDO.class);
        sysMsgConfigDO.setModifyBy(userId);
        sysMsgConfigDO.setModifyTime(LocalDateTime.now());
        return sysMsgConfigMapper.updateById(sysMsgConfigDO) > 0;
    }

    @Override
    public SysMsgConfigVO view(Long id) {
        SysMsgConfigDO sysMsgConfigDO = sysMsgConfigMapper.selectById(id);
        if(sysMsgConfigDO == null) {
            throw new BusinessException("无法获取缺省的邮件服务配置");
        }
        SysMsgConfigVO sysMsgConfigVO = BeanUtil.convertBean(sysMsgConfigDO, SysMsgConfigVO.class);
        sysMsgConfigVO.setReceiverTypeDesc(MsgReceiverTypeEnum.getNameByCode(sysMsgConfigVO.getReceiverType()));
        sysMsgConfigVO.setPublishStatusName(PublishStatusEnum.getName(sysMsgConfigVO.getPublishStatus()));
        return sysMsgConfigVO;
    }

    @Override
    public SysMsgConfigBO queryConfigByReceiverType(Integer receiverType) {
        SysMsgConfigDO sysMsgConfigDO = sysMsgConfigMapper.selectOne(new LambdaQueryWrapper<SysMsgConfigDO>().eq(SysMsgConfigDO::getReceiverType,receiverType));
        SysMsgConfigBO sysMsgConfigBO = BeanUtil.convertBean(sysMsgConfigDO, SysMsgConfigBO.class);

        return sysMsgConfigBO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changePublishStatus(Long id, Integer status) {
        Long userId = UserUtils.getUserId();
        SysMsgConfigDO sysMsgConfigDO =  sysMsgConfigMapper.selectById(id);
        if(sysMsgConfigDO == null){
            throw new BusinessException("记录不存在");
        }
        sysMsgConfigDO.setPublishStatus(status);
        sysMsgConfigDO.setModifyBy(userId);
        sysMsgConfigDO.setModifyTime(LocalDateTime.now());
        return sysMsgConfigMapper.updateById(sysMsgConfigDO) > 0;
    }

}
